﻿using System;
using System.Data.Common;
using System.Reflection;
using System.Security.Permissions;

namespace System.Data.EffiProz
{
  
        /// <summary>
        /// SQLite implementation of DbProviderFactory.
        /// </summary>
        public sealed partial class EfzFactory : IServiceProvider
        {
            private static Type _dbProviderServicesType;
            private static object _sqliteServices;

            static EfzFactory()
            {
                _dbProviderServicesType = Type.GetType("System.Data.Common.DbProviderServices, System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", false);
            }

            /// <summary>
            /// Will provide a DbProviderServices object in .NET 3.5
            /// </summary>
            /// <param name="serviceType">The class or interface type to query for</param>
            /// <returns></returns>
            object IServiceProvider.GetService(Type serviceType)
            {
                if ((_dbProviderServicesType != null && serviceType == _dbProviderServicesType))
                {
                    return GetSQLiteProviderServicesInstance();
                }
                return null;
            }

            [ReflectionPermission(SecurityAction.Assert, MemberAccess = true)]
            private object GetSQLiteProviderServicesInstance()
            {
                if (_sqliteServices == null)
                {
                    Type type = Type.GetType("System.Data.EffiProz.EfzProviderServices, System.Data.EffiProz.Linq, Version=0.1.0.1, Culture=neutral, PublicKeyToken=89e74d577370ac3d", false);
                    if (type != null)
                    {
                        FieldInfo field = type.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
                        _sqliteServices = field.GetValue(null);
                    }
                }
                return _sqliteServices;
            }
        }
 
}
